package demo.utils;

import demo.entity.tools.ServiceCodeEnum;
import demo.entity.tools.ServiceException;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.*;

/**
 * @Author Zhengx
 * @Date 2019/9/20
 * @Description
 */
@Slf4j
public class LocalDateUtil {

    private static final String DATE = "yyyy-MM-dd";

    private static final String TIME = "yyyy-MM-dd HH:mm:ss";

    private LocalDateUtil() {
    }

    public static final ExecutorService THREAD_POOL = new ThreadPoolExecutor(10, 20,
            0L, TimeUnit.SECONDS, new LinkedBlockingDeque<>(1024), (ThreadFactory) Thread::new);


    /**
     * 获取上个月第一天
     **/
    public static void getLastMonthFirstDay() {
        SimpleDateFormat format = new SimpleDateFormat(DATE);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        log.info("上个月第一天-{}", format.format(calendar.getTime()));
    }


    /**
     * 获取上个月最后一天
     **/
    public static void getLastMonthLastDay() {
        SimpleDateFormat sf = new SimpleDateFormat(DATE);
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        log.info("上个月最后一天-{}", sf.format(calendar.getTime()));
    }

    /**
     * 获取当月有哪些工作日
     **/
    public static List<Date> getDates(int year, int month) {
        List<Date> dates = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DATE, 1);
        while (cal.get(Calendar.YEAR) == year &&
                cal.get(Calendar.MONTH) < month) {
            int day = cal.get(Calendar.DAY_OF_WEEK);
            if (!(day == Calendar.SUNDAY || day == Calendar.SATURDAY)) {
                dates.add((Date) cal.getTime().clone());
            }
            cal.add(Calendar.DATE, 1);
        }
        return dates;

    }

    /**
     * 获取指定时间 2019-08-08 00:00:00
     **/
    public static Date getScheduleTime(String dateStr) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TIME);
        Date dateParse;
        try {
            dateParse = simpleDateFormat.parse(dateStr);
        } catch (ParseException e) {
            throw new ServiceException(ServiceCodeEnum.FAIL.getCode(), "时间解析异常");
        }
        return dateParse;
    }

    /**
     * 根据时间戳获取指定时间
     **/
    public static Date getScheduleTimeByTimestamp(Long time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TIME);
        String timeStr = simpleDateFormat.format(time);
        Date date;
        try {
            date = simpleDateFormat.parse(timeStr);
        } catch (ParseException e) {
            throw new ServiceException(ServiceCodeEnum.FAIL.getCode(), "时间解析异常");
        }
        return date;
    }

    public static String getFirstDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最小天数
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat(DATE);
        String firstDayOfMonth = sdf.format(cal.getTime());
        return firstDayOfMonth + " 00:00:00";
//        return "2019-10-08 00:00:00";
    }

    public static String getLastDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat(DATE);
        String lastDayOfMonth = sdf.format(cal.getTime());
        return lastDayOfMonth + " 23:59:59";
    }

    /**
     * 是否是周末
     **/
    public static boolean isWeekend(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY;
    }


    /**
     * 根据指定月份获取月初和月末
     **/
    public static Map<String, String> getMonthMap(int month) {
        Map<String, String> monthMap = new HashMap<>();
        SimpleDateFormat format = new SimpleDateFormat(TIME);
        // 获取前月的第一天
        Calendar cal = Calendar.getInstance();// 获取当前日期
        cal.add(Calendar.MONTH, -month);
        cal.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
        String firstDay = format.format(cal.getTime());
        // 获取前月的最后一天
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, -(month - 1));
        cale.set(Calendar.DAY_OF_MONTH, 0);
        String lastDay = format.format(cale.getTime());
        monthMap.put("beginOfMonth", firstDay);
        monthMap.put("endOfMonth", lastDay);
        return monthMap;
    }

}
